package com.tanhua.app.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.api.db.BlackListApi;
import com.tanhua.api.db.QuestionApi;
import com.tanhua.api.db.SettingsApi;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.PageBeanVo;
import com.tanhua.domain.vo.SettingsVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public class SettingsService {

    @Reference
    private QuestionApi questionApi;
    @Reference
    private SettingsApi settingsApi;
    @Reference
    private BlackListApi blackListApi;

    //查询登录用户的陌生人问题和通知喜好
    public ResponseEntity findUserSetting() {

        //准备一个app需要的vo对象
        SettingsVo vo = new SettingsVo();
        // 获取当前登录用户的id
        User user = UserHolder.getUser();
        // 根据当前登录用户查询登录人的问题
        Question question = questionApi.findUserStr(user.getId());
        if(question!=null){
            vo.setStrangerQuestion(question.getStrangerQuestion());
        }
        // 根据当前登录用户查询登录人的喜好通知
        Settings userSetting = settingsApi.findUserSetting(user.getId());
        if(userSetting!=null){
            vo.setPinglunNotification(userSetting.getPinglunNotification());
            vo.setLikeNotification(userSetting.getLikeNotification());
            vo.setGonggaoNotification(userSetting.getGonggaoNotification());
        }
        // 组装app需要的vo数据
        vo.setPhone(user.getPhone());
        vo.setId(user.getId());

        return ResponseEntity.ok(vo);
    }

    //新增/修改登录用户的陌生人问题
    public ResponseEntity settingUserQuestion(String content) {
        // 1 先判断登录用户有没有设置问题
        User user = UserHolder.getUser();
        Question question = questionApi.findUserStr(user.getId());
        if(question==null){
            // 2 没有--新增
            question=new Question();
            question.setUserId(user.getId());
            question.setStrangerQuestion(content);
            questionApi.save(question);
        }else{
            // 3 有--更新
            question.setStrangerQuestion(content);
            questionApi.update(question);
        }

        return ResponseEntity.ok(null);
    }

    //新增/修改登录用户的通知喜好
    public ResponseEntity userlikeSetting(Settings settings) {

        // 1 判断登录用户是否有设置过通知喜好
        User user = UserHolder.getUser();
        Settings userSetting = settingsApi.findUserSetting(user.getId());
        if(userSetting==null){
            // 没有 --新增settings
            settings.setUserId(user.getId());
            settingsApi.save(settings);
        }else{
            // 有-- 修改查询出来的settings
            userSetting.setPinglunNotification(settings.getPinglunNotification());
            userSetting.setLikeNotification(settings.getLikeNotification());
            userSetting.setGonggaoNotification(settings.getGonggaoNotification());
            settingsApi.update(userSetting);
        }

        return ResponseEntity.ok(null);
    }

    //分页查询登录用户的黑名单
    public ResponseEntity findPage(Integer page, Integer pagesize) {
        User user = UserHolder.getUser();
        //获取登录用户的用户id
        Long userId =user.getId();
        IPage<UserInfo> pb=blackListApi.findPage(userId,page,pagesize);

        //前端的问题：前端要的pagebean不是你后台使用技术查出来的pageBean
        //人家要的是人家自己的pageBean
        //将我们后台技术查出来的pageBean封装成人家前台要的pageBean
        PageBeanVo vo = new PageBeanVo(page, pagesize, pb.getTotal(), pb.getRecords());

        return ResponseEntity.ok(vo);
    }

    public ResponseEntity deleteById(Long UserId, Long blackId) {
        blackListApi.deleteById(UserId,blackId);
        return  ResponseEntity.ok(null);
    }
}
